using System;
using System.Web.Configuration;


namespace miPics.Common
{

	public class Config
	{
		public class General
		{
			public static string GalleryUrl
			{
				get {
					const string Keyname = "miPics.Url";

					string GalleryUrl = GetConfigKey(Keyname);

					if (string.IsNullOrEmpty(GalleryUrl)) ThrowConfigError(Keyname);

					return GalleryUrl;
				}
			}

			public static bool AlwaysLoggedIn
			{
				get { return (GetConfigKey("miPics.AlwaysLoggedIn") == "true"); }
			}

			public static string NoGalleryImageSrc
			{
				get { return "~/_Resource/NoGalleryImage.jpg"; }
			}

			public static string NoGalleryImageName
			{
				get { return "No Gallery Image"; }
			}
		}

		public class Image
		{
			public static short Width
			{
				get
				{
					const string Keyname = "miPics.Image.Width";

					short Width = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if (Width < 1) ThrowConfigError(Keyname);

					return Width;
				}
			}

			public static short Height
			{
				get
				{
					const string Keyname = "miPics.Image.Height";

					short Height = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if (Height < 1) ThrowConfigError(Keyname);

					return Height;
				}
			}

			public static short Quality
			{
				get
				{
					const string Keyname = "miPics.Image.Quality";

					short Quality = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if ((Quality < 0) || (Quality > 100))
						throw new Exception(Keyname + " must be between 1 and 100");

					return Quality;
				}
			}

			public static short WidthThumbnail
			{
				get
				{
					const string Keyname = "miPics.Thumbnail.Width";

					short Width = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if (Width < 1) ThrowConfigError(Keyname);

					return Width;
				}
			}

			public static short HeightThumbnail
			{
				get
				{
					const string Keyname = "miPics.Thumbnail.Height";

					short Height = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if (Height < 1) ThrowConfigError(Keyname);

					return Height;
				}
			}

			public static short QualityThumbnail
			{
				get
				{
					const string Keyname = "miPics.Thumbnail.Quality";

					short Quality = ToolKit.ReturnShort(GetConfigKey(Keyname), -1);

					if ((Quality < 0) || (Quality > 100))
						throw new Exception(Keyname + " must be between 1 and 100");

					return Quality;
				}
			}
		}

		public class Administration
		{
			public static string Username
			{
				get
				{
					const string Keyname = "miPics.Adminstration.Username";

					string Username = GetConfigKey(Keyname);

					if (Username == "") ThrowConfigError(Keyname);

					return Username;
				}
			}

			public static string Password
			{
				get
				{
					const string Keyname = "miPics.Adminstration.Password";

					string Password = GetConfigKey(Keyname);

					if (Password == "") ThrowConfigError(Keyname);

					return Password;
				}
			}
		}

		public class Rss
		{
			public static string Title
			{
				get {
					const string Keyname = "miPics.Rss.Title";

					string RssTitle = GetConfigKey(Keyname);

					if (RssTitle == "") ThrowConfigError(Keyname);

					return RssTitle;
				}
			}

			public static string Description
			{
				get {
					const string Keyname = "miPics.Rss.Description";

					string RssDescription = GetConfigKey(Keyname);

					if (RssDescription == "") ThrowConfigError(Keyname);

					return RssDescription;
				}
			}
		}

		private static string GetConfigKey(string KeyName)
		{
			return WebConfigurationManager.AppSettings[KeyName].ToString().Trim();
		}

		private static void ThrowConfigError(string Keyname)
		{
			throw new Exception("Key [" + Keyname + "] is not specified in Web.config");
		}
	}

}